package com.imooc.api;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @version 1.0
 * @Author Mr.Yang
 * @Date 2024-08-14 19:06
 * @question: AOP日志实现每个接口接口运行的具体时间
 */

@Slf4j
@Component
@Aspect
public class ServiceLogAspect {

    @Around("execution(* com.imooc.service.impl..*.*(..))")
    public Object recordTimeLog(ProceedingJoinPoint joinPoint) throws Throwable {

//        StopWatch stopWatch = new StopWatch();

//        org.apache.commons.lang3.time.StopWatch watch = org.apache.commons.lang3.time.StopWatch.createStarted();
//        watch.split();
//        watch.getSplitTime();
//        watch.split();
//        watch.getSplitTime();
//        watch.split();
//        watch.getSplitTime();
//        watch.split();
//        watch.getSplitTime();

        long begin = System.currentTimeMillis();

//        stopWatch.start("task1");

        Object proceed = joinPoint.proceed();
        //某个接口的整路径
        //比如: com.imooc.service.impl.AdminServiceImpl.A()
        String point = joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName();
//        stopWatch.stop();

//        stopWatch.start("task2");
//        Thread.sleep(350);
//        stopWatch.stop();

//        stopWatch.start("task3");
//        Thread.sleep(550);
//        stopWatch.stop();

//        log.info(stopWatch.prettyPrint());
//        log.info(stopWatch.shortSummary());
//        System.out.println(stopWatch.getTotalTimeMillis());
//        System.out.println(stopWatch.getTaskCount());

        long end = System.currentTimeMillis();
        long takeTime = end - begin;

        if (takeTime > 3000) {
            log.error("执行方法：{} 执行时间太长了，耗费了{}毫秒", point, takeTime);
        } else if (takeTime > 2000) {
            log.warn("执行方法：{} 执行时间稍微有点长，耗费了{}毫秒", point, takeTime);
        } else {
            log.info("执行方法：{} 执行时间，耗费了{}毫秒", point, takeTime);
        }

        return proceed;
    }


}

